//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet.Formats.Biff.Tokens
{
    internal class BinaryToken : EmptyFormulaToken
    {
        private readonly BinaryOperatorType _operator;

        public BinaryToken(BinaryOperatorType op)
        {
            _operator = op;
        }


        internal override Expression GetExpression(FormulaEvaluationContext context)
        {
            FormulaToken t1 = context.PoppedArgument();
            Expression right = t1.GetExpression(context);
            FormulaToken t2 = context.PoppedArgument();
            Expression left = t2.GetExpression(context);
            return new BinaryExpression(left, _operator, right);
        }

        public override FormulaTokenId TokenId
        {
            get {
                switch (_operator)
                {
                        case BinaryOperatorType.Add:
                        return FormulaTokenId.Add;
                        case BinaryOperatorType.And:
                        throw new NotSupportedException();
                        case BinaryOperatorType.Divide:
                        return FormulaTokenId.Div;
                        case BinaryOperatorType.Equality:
                        return FormulaTokenId.EQ;
                    case BinaryOperatorType.GreaterThan:
                        return FormulaTokenId.GT;
                        case BinaryOperatorType.GreaterThanOrEqual:
                        return FormulaTokenId.GE;
                        case BinaryOperatorType.Inequality:
                        return FormulaTokenId.NE;
                        case BinaryOperatorType.LessThan:
                        return FormulaTokenId.LT;
                    case BinaryOperatorType.LessThanOrEqual:
                        return FormulaTokenId.LE;
                    case BinaryOperatorType.Modulus:
                        throw new NotSupportedException();
                        case BinaryOperatorType.Multiply:
                        return FormulaTokenId.Mul;
                        case BinaryOperatorType.Or:
                        throw new NotSupportedException();
                        case BinaryOperatorType.Power:
                        return FormulaTokenId.Power;
                        case BinaryOperatorType.Subtract:
                        return FormulaTokenId.Sub;
                    default:
                        throw new NotSupportedException();
                } }
        }
    }
}